﻿namespace MEF_Test.Bricks.Actions
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;

    using MEF_Test.Contracts;

    using Microsoft.Practices.Unity;

    /// <summary>
    /// The built-in implementation of the <see cref="IActionsManager"/> interface. It contains lazy references to all the exported MEF-Parts representing actions applicable on an <see cref="IBrowserNodeViewModel"/>.
    /// </summary>
    public class ActionsManager : IActionsManager
    {
        #region Constants and Fields

        /// <summary>
        /// The backing field for the <see cref="ExportedActions"/> property.
        /// </summary>
        [ImportMany(typeof(INodeAction), RequiredCreationPolicy = CreationPolicy.Shared)]

        // Resharper doesn't know MEF, so there is no way to make this warning disappear.
        private ObservableCollection<Lazy<INodeAction, IDictionary<string, object>>> _exportedActions;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ActionsManager"/> class.
        /// </summary>
        public ActionsManager()
        {
            // get the application-wide composition container.
            var container = IoC.Current.Resolve<CompositionContainer>();

            // import the parts.
            container.ComposeParts(this);                       
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the exported actions.
        /// </summary>
        /// <value>The exported actions.</value>        
        public ObservableCollection<Lazy<INodeAction, IDictionary<string, object>>> ExportedActions
        {
            get
            {
                return this._exportedActions;
            }
        }

        #endregion
    }
}
